package com.zx.onlineeducation.service.cms.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * MyBatis Plus 配置类
 *
 * @author 郑雪
 * @since 2022-01-07
 */
@Configuration
@MapperScan("com.zx.onlineeducation.service.cms.mapper")
@EnableTransactionManagement
public class MyBatisPlusConfig {

    /**
     * MyBatis Plus 拦截器
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {

        // 1.创建 MyBatis Plus 拦截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 2.创建分页插件
        PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
        // ①设置数据库类型
        paginationInterceptor.setDbType(DbType.MYSQL);
        // ②设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
         paginationInterceptor.setOverflow(true);
        // ③设置最大单页限制数量，默认 500 条，-1 不受限制
         paginationInterceptor.setMaxLimit(500L);
        // ④开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setOptimizeJoin(true);
        // ⑤添加分页插件
        interceptor.addInnerInterceptor(paginationInterceptor);

        // 3.创建乐观锁插件
        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
        // 添加乐观锁插件
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);

        // 返回拦截器
        return interceptor;
    }

}
